﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Dqaqs.Web.Models;

namespace Dqaqs.Web.Controllers
{
	public class MenuController : Controller
	{
		private static IEnumerable<MenuItem> _menus;
		//
		// GET: /Menu/

		static MenuController()
		{
			var projects = new MenuItem { Title = "Projects", Controller = "Project", Action = "Index" };
			projects.SubMenus = new[]
			                    	{
			                    		new MenuItem {Title = "Select Project", Controller = "Project", Action = "List", SubMenus = null},
			                    		new MenuItem {Title = "New Project", Controller = "Project", Action = "New", SubMenus = null},
			                    		new MenuItem {Title = "Project Details", Controller = "Project", Action = "Detail", SubMenus = null},
			                    		new MenuItem {Title = "Edit Project", Controller = "Project", Action = "Edit", SubMenus = null},
			                    	};

			var schema = new MenuItem { Title = "Global Schema", Controller = "Project", Action = "Schema", SubMenus = null };

			var ds = new MenuItem { Title = "Data Sources", Controller = "DataSource", Action = "Index" };
			ds.SubMenus = new[]
			              	{
			              		new MenuItem
			              			{Title = "Create New Data Source", Controller = "DataSource", Action = "New", SubMenus = null},
			              		new MenuItem
			              			{Title = "List Data Source", Controller = "DataSource", Action = "Index", SubMenus = null},
			              	};
			_menus = new[] { projects, schema, ds };
		}

		public ActionResult Items()
		{
			return PartialView(_menus);
		}

		private static IEnumerable<JsonMenuItem> FormatMenus(IEnumerable<MenuItem> menus)
		{
			return
				menus.Select(m => new JsonMenuItem { Title = m.Title, Action = String.Format("{0}.{1}", m.Controller, m.Action) });
		}

		public ActionResult SubItems(string item)
		{
			var subMenus = _menus.FirstOrDefault(m => m.Title == item);
			if (subMenus == null || subMenus.SubMenus == null)
				return PartialView(Enumerable.Empty<MenuItem>());
			return PartialView(subMenus.SubMenus);
		}

	}
}
